package cn.iocoder.yudao.module.alipay.controller.admin.sensitivenems;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.sensitivenems.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.sensitivenems.SensitiveNemsDO;
import cn.iocoder.yudao.module.alipay.convert.sensitivenems.SensitiveNemsConvert;
import cn.iocoder.yudao.module.alipay.service.sensitivenems.SensitiveNemsService;

@Tag(name = "管理后台 - 敏感词")
@RestController
@RequestMapping("/alipay/sensitive-nems")
@Validated
public class SensitiveNemsController {

    @Resource
    private SensitiveNemsService sensitiveNemsService;

    @PostMapping("/create")
    @Operation(summary = "创建敏感词")
    @PreAuthorize("@ss.hasPermission('alipay:sensitive-nems:create')")
    public CommonResult<Integer> createSensitiveNems(@Valid @RequestBody SensitiveNemsCreateReqVO createReqVO) {
        return success(sensitiveNemsService.createSensitiveNems(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新敏感词")
    @PreAuthorize("@ss.hasPermission('alipay:sensitive-nems:update')")
    public CommonResult<Boolean> updateSensitiveNems(@Valid @RequestBody SensitiveNemsUpdateReqVO updateReqVO) {
        sensitiveNemsService.updateSensitiveNems(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除敏感词")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:sensitive-nems:delete')")
    public CommonResult<Boolean> deleteSensitiveNems(@RequestParam("id") Integer id) {
        sensitiveNemsService.deleteSensitiveNems(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得敏感词")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:sensitive-nems:query')")
    public CommonResult<SensitiveNemsRespVO> getSensitiveNems(@RequestParam("id") Integer id) {
        SensitiveNemsDO sensitiveNems = sensitiveNemsService.getSensitiveNems(id);
        return success(SensitiveNemsConvert.INSTANCE.convert(sensitiveNems));
    }

    @GetMapping("/list")
    @Operation(summary = "获得敏感词列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:sensitive-nems:query')")
    public CommonResult<List<SensitiveNemsRespVO>> getSensitiveNemsList(@RequestParam("ids") Collection<Integer> ids) {
        List<SensitiveNemsDO> list = sensitiveNemsService.getSensitiveNemsList(ids);
        return success(SensitiveNemsConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得敏感词分页")
    @PreAuthorize("@ss.hasPermission('alipay:sensitive-nems:query')")
    public CommonResult<PageResult<SensitiveNemsRespVO>> getSensitiveNemsPage(@Valid SensitiveNemsPageReqVO pageVO) {
        PageResult<SensitiveNemsDO> pageResult = sensitiveNemsService.getSensitiveNemsPage(pageVO);
        return success(SensitiveNemsConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出敏感词 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:sensitive-nems:export')")
    @OperateLog(type = EXPORT)
    public void exportSensitiveNemsExcel(@Valid SensitiveNemsExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SensitiveNemsDO> list = sensitiveNemsService.getSensitiveNemsList(exportReqVO);
        // 导出 Excel
        List<SensitiveNemsExcelVO> datas = SensitiveNemsConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "敏感词.xls", "数据", SensitiveNemsExcelVO.class, datas);
    }

}
